package com.lanou3g.k12.web.controller;


import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.lanou3g.k12.entity.ArticleCategory;
import com.lanou3g.k12.service.ArticleCategoryService;
import com.lanou3g.k12.utils.K12Result;
import com.lanou3g.k12.utils.K12ResultUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;


@RestController
public class ArticleCategoryController {

    @Autowired
    ArticleCategoryService articleCategoryService;

    // 获取资讯分类列表
    @GetMapping("/article/categories")
    public K12Result findArticleCategoryByKeyword(String keyword,@RequestParam(value = "page",defaultValue = "1") Integer page,@RequestParam(value = "size",defaultValue = "30") Integer size){

        Page<Object> objects;
        if (keyword!=null&& !keyword.trim().equals("")){
            objects = PageHelper.startPage(page, size);
            articleCategoryService.findByKeyword(page, size, keyword);
            return K12ResultUtils.ok(objects);

        }
        else {
            objects = PageHelper.startPage(page, size);
            articleCategoryService.findAll(page, size);
            return K12ResultUtils.ok(objects);
        }


    }




    //通过ID批量删除资讯分类
    @DeleteMapping("/article/categories/{ids}")
    public K12Result deleteArticleCategoryByIds(@PathVariable Integer[] ids){

        articleCategoryService.deleteArticleCategoryByIds(ids);

        return K12ResultUtils.ok();
    }


    //  新增资讯分类
    @PostMapping("/article/category")

    public K12Result insertArticleCategory(String name,String remark,Integer status){


        articleCategoryService.insertArticleCategory(name,remark,status);


        return K12ResultUtils.ok();
    }

    //通过ID获取资讯分类
    @GetMapping("/article/category/{id}")
    public K12Result findArticleCategoryById(@PathVariable Long id){

        ArticleCategory articleCategory = articleCategoryService.findArticleCategoryById(id);

        return K12ResultUtils.ok(articleCategory);
    }


    // 通过ID修改资讯分类

    @PutMapping("/article/category/{id}")
    public K12Result updateArticleCategoryById(@PathVariable Long id, String name,String remark,Integer status){

        articleCategoryService.updateArticleCategoryById(id,name,remark,status);

        return K12ResultUtils.ok();
    }



    // 通过ID删除资讯分类
    @DeleteMapping("/article/category/{id}")
    public K12Result deleteArticleCategoryById(@PathVariable Long id){

        articleCategoryService.deleteArticleCategoryById(id);

        return K12ResultUtils.ok();
    }



    // 获取资讯分类列表(后端)
    @GetMapping("/article/categories/backend")
    public K12Result findByKeywordForBackend(String keyword,@RequestParam(value = "page",defaultValue = "1") Integer page,@RequestParam(value = "size",defaultValue = "30") Integer size){

        Page<Object> objects;
        if (keyword!=null&& !keyword.trim().equals("")){
            objects = PageHelper.startPage(page, size);
            articleCategoryService.findByKeywordForBackend(page, size, keyword);
            return K12ResultUtils.ok(objects);

        }
        else {
            objects = PageHelper.startPage(page, size);
            articleCategoryService.findAllForBackend(page, size);
            return K12ResultUtils.ok(objects);
        }


    }


    //通过ID获取资讯分类(后端)
    @GetMapping("/article/category/backend/{id}")
    public K12Result findByIdForBackend(@PathVariable Long id){

        ArticleCategory articleCategory = articleCategoryService.findByIdForBackend(id);

        return K12ResultUtils.ok(articleCategory);
    }

    }

